package algotithm.leetcode.bytedance.test20;

import java.util.Stack;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/9-22:21
 * @URL https://leetcode-cn.com/problems/valid-parentheses/
 * @Title 20. 有效的括号
 */
public class Solution {
    public boolean isValid(String s) {
        if(s.length()%2==0){
            Stack<Character> stack=new Stack<>();
            for(int i=0;i<s.length();i++){
                char c=s.charAt(i);
                if(c=='('||c=='['||c=='{'){
                    stack.push(c);
                } else if(c==')') {
                    if(!stack.isEmpty()&&stack.peek()=='('){stack.pop();}
                    else{return false;}
                } else if(c=='}') {
                    if(!stack.isEmpty()&&stack.peek()=='{'){stack.pop();}
                    else{return false;}
                } else {
                    if(!stack.isEmpty()&&stack.peek()=='['){stack.pop();}
                    else{return false;}
                }
            }
            return stack.isEmpty();
        }
        return false;
    }
}